// controller/user.js
"use strict";
const Controller = require("egg").Controller;
const moment = require("moment");
class UserController extends Controller {
  // 用户登录
  async userLogin() {
    const { ctx, app } = this;
    ctx.logger.info("用户登录参数", ctx.request.body);
    const { username, password } = ctx.request.body;
    const userInfo = await ctx.service.user.getUserByName(username);
    if (!userInfo || !userInfo.id) {
      ctx.body = {
        code: 500,
        msg: "账号不存在",
        success: false,
        data: null,
      };
      return;
    }
    if (userInfo && password !== userInfo.password) {
      ctx.body = {
        code: 500,
        msg: "账号密码错误",
        success: false,
        data: null,
      };
      return;
    }
    // token 生成
    const token = app.jwt.sign(
      {
        id: userInfo.id,
        username: userInfo.username,
        exp: Math.floor(Date.now() / 1000) + 1 * 60 * 60, // token 有效期为 1 小时
      },
      app.config.jwt.secret
    );
    ctx.logger.info('user-token 生成', token);
    ctx.body = {
      code: 200,
      message: "登录成功",
      success: true,
      data: {
        token,
      },
    };
  }

  //获取个人信息
  async getUserInfo() {
    const { ctx, app } = this;
    const tokenResult = ctx.middlewareTokenResult
    ctx.logger.info("tokenResult", tokenResult);
    try {
      const userInfo = await ctx.service.user.getUserByName(tokenResult.username);
      const defaultAvatar = "https://profile.csdnimg.cn/4/2/E/3_weixin_38992765";
      ctx.body = {
        code: 200,
        msg: "请求成功",
        data: {
          id: userInfo.id,
          username: userInfo.username,
          signature: userInfo.signature || "",
          avatar: userInfo.avatar || defaultAvatar,
          nickname: userInfo.nickname || "不知名先生",
        },
      };
    } catch (error) {
      ctx.status = 500;
      ctx.body = {
        code: 500,
        msg: "请求头authorization授权为空，请求失败",
        data: null,
      };
    }
  }

  // 注册
  async register() {
    const { ctx } = this;
    const { username, password, nickname } = ctx.request.body;
    const userInfo = await ctx.service.user.getUserByName(username);
    // 判断是否已经存在
    if (userInfo && userInfo.id) {
      ctx.body = {
        code: 500,
        msg: "账户名已被注册，请重新输入",
        data: null,
        success: false,
      };
      return;
    }
    const defaultAvatar = "https://profile.csdnimg.cn/4/2/E/3_weixin_38992765";
    let requestData = ctx.request.body;
    !requestData.permissions && (requestData.permissions = 0);
    !requestData.create_time && (requestData.create_time = moment().format("YYYY-MM-DD HH:mm:ss"));
    !requestData.signature && (requestData.signature = "暂无个人签名");
    !requestData.avatar && (requestData.avatar = defaultAvatar);
    const result = await ctx.service.user.register(requestData);
    if (result) {
      ctx.body = {
        code: 200,
        msg: "注册成功",
        data: null,
        success: true,
      };
    } else {
      ctx.body = {
        code: 500,
        msg: "注册失败",
        data: null,
        success: false,
      };
    }
  }
}

module.exports = UserController;
